//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO.Packaging;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Serialization;

namespace OpenLS.Vml.Drawing
{
    internal class OleObject : IOfficeXmlSerializable
    {
        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(VmlConstants.OfficeNamespace + "OLEObject"))
            {
                _Type = c.GetOptionalEnum<OleType>("Type");
                ProgID = c.GetOptionalString("ProgID");
                ShapeID = c.GetOptionalString("ShapeID");
                DrawAspect = c.GetOptionalEnum<OleDrawAspect>("DrawAspect");
                ObjectID = c.GetOptionalString("ObjectID");
                string rid = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (rid != null)
                {
                    var relationship = c.Part.GetRelationship(rid);
                    PackagePart newPart = c.Part.Package.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
                    using (var stream = newPart.GetStream())
                    {
                        _data = new byte[stream.Length];
                        stream.Read(_data, 0, _data.Length);
                    }

                }
                UpdateMode = c.GetOptionalEnum<OleUpdateMode>("UpdateMode");
                _linkType = c.ReadOptionalElementEnumAttribute<OleLinkType>("LinkType");
                c.WarnIf("LockedField");
                _fieldCodes = c.ReadOptionalElementString("FieldCodes");
            }
        }

        private OleType? _Type;
        private string ProgID;
        private OleUpdateMode? UpdateMode;
        private string ObjectID;
        private OleDrawAspect? DrawAspect;
        private string ShapeID;
        private byte[] _data;
        private string _fieldCodes;
        private OleLinkType? _linkType;

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(VmlConstants.OfficeNamespace + "OLEObject"))
            {
                c.SetOptionalEnum("Type", _Type);
                c.SetOptionalString("ProgID", ProgID);
                c.SetOptionalString("ShapeID", ShapeID);
                c.SetOptionalEnum("DrawAspect", DrawAspect);
                c.SetOptionalString("ObjectID", ObjectID);
                if (_data != null)
                {
                    var blipUri = context.GetAvailableUri("_data{0}" + ".bin");
                    var blipPart = context.Part.Package.CreatePart(blipUri, "data/" + ".bin"); //\\OK?
                    var rel =
                        context.Part.CreateRelationship(blipUri, TargetMode.Internal,
                                                        OfficeConstants.ImageRelationshipType);
                    using (var stream = blipPart.GetStream())
                    {
                        stream.Write(_data, 0, _data.Length);
                    }
                }
                c.SetOptionalEnum<OleUpdateMode>("UpdateMode", UpdateMode);
                c.WriteOptionalElementEnumAttribute("LinkType", _linkType);
                c.WarnIf("LockedField");
                c.WriteOptionalElementString("FieldCodes", _fieldCodes);
            }
        }
    }
}